"""
Case Type   : fullsql
Case Name   : track_stmt_details_size参数gs_guc reload异常输入校验,
             默认值为4096,取值范围为0-100000000
Create At   : 2022/02/07
Owner       : zhao-pengcheng-g
Description :
    1.设置为最小值0，查看参数
    2.设置为最大值100000000，查看参数
    3.设置为-1，查看参数
    4.设置为取值范围外的值，查看参数
    5.设置为字符值，查看参数
    6.设置为特殊字符，查看参数
    7.设置为带单位的值，查看参数
    8.恢复参数
Expect      :
    1.设置成功,查看参数结果为0
    2.设置成功，查看参数结果为100000000
    3.设置失败，合理报错，查看参数结果为100000000
    4.设置失败，合理报错，查看参数结果为100000000
    5.设置失败，合理报错，查看参数结果为100000000
    6.设置失败，合理报错，查看参数结果为100000000
    7.设置失败，合理报错，查看参数结果为100000000
    8.恢复参数成功
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class Fullsql(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        text = '---Opengauss_Function_Fullsql_Case0028 start---'
        self.logger.info(text)
        self.userNode = Node('PrimaryDbUser')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()

    def test_fullsql(self):
        text = '---step1:设置为最小值0，查看参数,' \
               'expect:设置成功,查看参数结果为0---'
        self.logger.info(text)
        self.config_size = 'track_stmt_details_size'
        self.show_size_cmd = f'show {self.config_size};'
        check_size = self.sh_primy.execut_db_sql(self.show_size_cmd)
        self.logger.info(check_size)
        self.size_default = check_size.splitlines()[-2].strip()
        guc_size = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_size}=0")
        self.assertTrue(guc_size, '执行失败:' + text)
        check_size_msg = self.sh_primy.execut_db_sql(self.show_size_cmd)
        self.logger.info(check_size_msg)
        self.common.equal_sql_mdg(check_size_msg, self.config_size,
                                  '0', '(1 row)', flag='1')

        text = "---step2:设置为最大值100000000,查看参数," \
               "expect:设置成功,查看参数结果为100000000---"
        self.logger.info(text)
        guc_size = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_size}=100000000")
        self.assertTrue(guc_size, '执行失败:' + text)
        show_size_msg = self.sh_primy.execut_db_sql(self.show_size_cmd)
        self.logger.info(show_size_msg)
        self.common.equal_sql_mdg(show_size_msg, self.config_size,
                                  '100000000', '(1 row)', flag='1')

        text = '---step3:设置为-1，查看参数,expect:设置失败，合理报错，' \
               '查看参数结果为100000000---'
        self.logger.info(text)
        guc_size = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_size}=-1",
                                               get_detail=True)
        self.assertIn(self.constant.OUTSIDE_VALID_RANGE_MSG,
                      guc_size, '执行失败:' + text)
        show_size_msg = self.sh_primy.execut_db_sql(self.show_size_cmd)
        self.logger.info(show_size_msg)
        self.common.equal_sql_mdg(show_size_msg, self.config_size,
                                  '100000000', '(1 row)', flag='1')

        text = '---step4:设置为取值范围外的值，查看参数,' \
               'expect:设置失败，合理报错，查看参数结果为100000000---'
        self.logger.info(text)
        guc_size = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_size}=5764607523034234870",
                                               get_detail=True)
        self.assertIn(self.constant.OUTSIDE_VALID_RANGE_MSG,
                      guc_size, '执行失败:' + text)
        show_size_msg = self.sh_primy.execut_db_sql(self.show_size_cmd)
        self.logger.info(show_size_msg)
        self.common.equal_sql_mdg(show_size_msg, self.config_size,
                                  '100000000', '(1 row)', flag='1')

        text = '---step5:设置为字符值，查看参数,' \
               'expect:设置失败，合理报错，查看参数结果为100000000---'
        self.logger.info(text)
        guc_size = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_size}='abcd'",
                                               get_detail=True)
        self.assertIn(self.constant.requires_integer_value_msg,
                      guc_size, '执行失败:' + text)
        show_size_msg = self.sh_primy.execut_db_sql(self.show_size_cmd)
        self.logger.info(show_size_msg)
        self.common.equal_sql_mdg(show_size_msg, self.config_size,
                                  '100000000', '(1 row)', flag='1')

        text = '---step6:设置为特殊字符，查看参数,' \
               'expect:设置失败，合理报错，查看参数结果为100000000---'
        self.logger.info(text)
        guc_size = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_size}='!#$%@#%$!^'",
                                               get_detail=True)
        self.assertIn("The parameter('!#$%@#%^') exists illegal character:#",
                      guc_size, '执行失败:' + text)
        show_size_msg = self.sh_primy.execut_db_sql(self.show_size_cmd)
        self.logger.info(show_size_msg)
        self.common.equal_sql_mdg(show_size_msg, self.config_size,
                                  '100000000', '(1 row)', flag='1')

        text = '---step7:设置为带单位的值，查看参数,' \
               'expect:设置失败，合理报错，查看参数结果为100000000---'
        self.logger.info(text)
        guc_size = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_size}='10K'",
                                               get_detail=True)
        self.assertIn(self.constant.requires_integer_value_msg,
                      guc_size, '执行失败:' + text)
        show_size_msg = self.sh_primy.execut_db_sql(self.show_size_cmd)
        self.logger.info(show_size_msg)
        self.common.equal_sql_mdg(show_size_msg, self.config_size,
                                  '100000000', '(1 row)', flag='1')

    def tearDown(self):
        text = '---step8:恢复参数，expect:成功---'
        self.logger.info(text)
        check_size = self.sh_primy.execut_db_sql(self.show_size_cmd)
        self.logger.info(check_size)
        current = check_size.splitlines()[-2].strip()
        if self.size_default != current:
            result = self.sh_primy.execute_gsguc('reload',
                                                 self.constant.GSGUC_SUCCESS_MSG,
                                                 f'{self.config_size}={self.size_default}')
            self.assertTrue(result, '参数恢复失败')
        show_size_msg = self.sh_primy.execut_db_sql(self.show_size_cmd)
        self.logger.info(show_size_msg)
        self.common.equal_sql_mdg(show_size_msg, self.config_size,
                                  self.size_default, '(1 row)', flag='1')
        self.logger.info(
            '---Opengauss_Function_Fullsql_Case0028 finish---')
